package com.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncrypt {

    private static String privateKey;
    private static String publicKey;

    static {
        try {
            String[] parseKeyPair = ConfigTools.genKeyPair(512);
            privateKey = parseKeyPair[0];
            publicKey = parseKeyPair[1];

            System.out.println("privateKey:  " + privateKey);
            System.out.println("publicKey:  " + publicKey);
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    // 加密
    public static String encrypt(String password) throws Exception {
        return ConfigTools.encrypt(privateKey, password);
    }

    // 解密
    public static String decrypt(String encryptText) throws Exception {
        return ConfigTools.decrypt(publicKey, encryptText);
    }
    public static void main(String[] args) throws Exception {
        System.out.println("encrypt:  " + encrypt("1014"));
    }
}
